using System;
using System.Collections.Generic;
using System.Xml;
using System.Data;

namespace Microsoft.Health.Samples.PatientConnect.TreyResearch.HealthVaultAdapter
{
    /// <summary>
    /// Practice class provides properties and methods to add and select practice records.
    /// </summary>
    public class Practice
    {
        private Guid _practiceId = Guid.Empty;
        private string _practiceName = String.Empty;
        private Guid _childAppId = Guid.Empty;
        private string _dataFolder = String.Empty;
        private PracticeDBHandler _practiceDataSet = null;
        private string _practiceXml = String.Empty;

        /// <summary>
        /// Constructor for the Practice class.
        /// </summary>
        /// <param name="xmlFileName">Practice's xml file name.</param>
        public Practice(string xmlFileName)
        {
            _practiceXml = xmlFileName;
            _practiceDataSet = new PracticeDBHandler(xmlFileName);
        }

        /// <summary>
        /// Practice Id of the practice
        /// </summary>
        public Guid PracticeId
        {
            get { return _practiceId; }
            set { _practiceId = value; }
        }

        /// <summary>
        /// Practice name of the practice
        /// </summary>
        public string PracticeName
        {
            get { return _practiceName; }
            set { _practiceName = value; }
        }

        /// <summary>
        /// Child application id of the practice
        /// </summary>
        public Guid ChildAppId
        {
            get { return _childAppId; }
            set { _childAppId = value; }
        }

        /// <summary>
        /// Data folder of the practice.
        /// </summary>
        public string DataFolder
        {
            get { return _dataFolder; }
            set { _dataFolder = value; }
        }

        /// <summary>
        /// Adds new practice record to the xml file.
        /// </summary>
        public void AddPractice()
        {
            _practiceDataSet.AddPractice(this.PracticeId, this.PracticeName, this.ChildAppId, this.DataFolder);
        }

        /// <summary>
        /// Gets list of practice records according to the conditions
        /// </summary>
        /// <param name="condition">Condition to select practice records.</param>
        /// <returns>List of Practice records</returns>
        public List<Practice> SelectPracticeRecords(string condition)
        {
            List<Practice> practiceList = null;
            Practice practice;
            int rowCount = 0;

            DataRow[] practiceRows = _practiceDataSet.SelectPractice(condition);
            rowCount = practiceRows.Length;
            practiceList = new List<Practice>();
            for (int counter = 0; counter < rowCount; counter++)
            {
                practice = new Practice(this._practiceXml);
                practice.PracticeId = (Guid)practiceRows[counter]["PracticeId"];
                practice.PracticeName = practiceRows[counter]["PracticeName"].ToString();
                practice.ChildAppId = (Guid)practiceRows[counter]["childAppId"];
                practice.DataFolder = practiceRows[counter]["DataFolder"].ToString();

                practiceList.Add(practice);
            }
            return practiceList;
        }
    }
}
